﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000263 RID: 611
	internal class CombineLatestObservable<TLeft, TRight, TResult> : OperatorObservableBase<TResult>
	{
		// Token: 0x06000DEC RID: 3564 RVA: 0x00041A00 File Offset: 0x0003FE00
		public CombineLatestObservable(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector) : base(left.IsRequiredSubscribeOnCurrentThread<TLeft>() || right.IsRequiredSubscribeOnCurrentThread<TRight>())
		{
			this.left = left;
			this.right = right;
			this.selector = selector;
		}

		// Token: 0x06000DED RID: 3565 RVA: 0x00041A31 File Offset: 0x0003FE31
		protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
		{
			return new CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest(this, observer, cancel).Run();
		}

		// Token: 0x040007DC RID: 2012
		private readonly IObservable<TLeft> left;

		// Token: 0x040007DD RID: 2013
		private readonly IObservable<TRight> right;

		// Token: 0x040007DE RID: 2014
		private readonly Func<TLeft, TRight, TResult> selector;

		// Token: 0x02000264 RID: 612
		private class CombineLatest : OperatorObserverBase<TResult, TResult>
		{
			// Token: 0x06000DEE RID: 3566 RVA: 0x00041A40 File Offset: 0x0003FE40
			public CombineLatest(CombineLatestObservable<TLeft, TRight, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000DEF RID: 3567 RVA: 0x00041A88 File Offset: 0x0003FE88
			public IDisposable Run()
			{
				IDisposable disposable = this.parent.left.Subscribe(new CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest.LeftObserver(this));
				IDisposable disposable2 = this.parent.right.Subscribe(new CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest.RightObserver(this));
				return StableCompositeDisposable.Create(disposable, disposable2);
			}

			// Token: 0x06000DF0 RID: 3568 RVA: 0x00041ACC File Offset: 0x0003FECC
			public void Publish()
			{
				if (!this.leftCompleted || this.leftStarted)
				{
					if (!this.rightCompleted || this.rightStarted)
					{
						if (!this.leftStarted || !this.rightStarted)
						{
							return;
						}
						TResult value;
						try
						{
							value = this.parent.selector(this.leftValue, this.rightValue);
						}
						catch (Exception error)
						{
							try
							{
								this.observer.OnError(error);
							}
							finally
							{
								base.Dispose();
							}
							return;
						}
						this.OnNext(value);
						return;
					}
				}
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000DF1 RID: 3569 RVA: 0x00041BA8 File Offset: 0x0003FFA8
			public override void OnNext(TResult value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06000DF2 RID: 3570 RVA: 0x00041BB8 File Offset: 0x0003FFB8
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000DF3 RID: 3571 RVA: 0x00041BF0 File Offset: 0x0003FFF0
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040007DF RID: 2015
			private readonly CombineLatestObservable<TLeft, TRight, TResult> parent;

			// Token: 0x040007E0 RID: 2016
			private readonly object gate = new object();

			// Token: 0x040007E1 RID: 2017
			private TLeft leftValue = default(TLeft);

			// Token: 0x040007E2 RID: 2018
			private bool leftStarted;

			// Token: 0x040007E3 RID: 2019
			private bool leftCompleted;

			// Token: 0x040007E4 RID: 2020
			private TRight rightValue = default(TRight);

			// Token: 0x040007E5 RID: 2021
			private bool rightStarted;

			// Token: 0x040007E6 RID: 2022
			private bool rightCompleted;

			// Token: 0x02000265 RID: 613
			private class LeftObserver : IObserver<TLeft>
			{
				// Token: 0x06000DF4 RID: 3572 RVA: 0x00041C28 File Offset: 0x00040028
				public LeftObserver(CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest parent)
				{
					this.parent = parent;
				}

				// Token: 0x06000DF5 RID: 3573 RVA: 0x00041C38 File Offset: 0x00040038
				public void OnNext(TLeft value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.leftStarted = true;
						this.parent.leftValue = value;
						this.parent.Publish();
					}
				}

				// Token: 0x06000DF6 RID: 3574 RVA: 0x00041C98 File Offset: 0x00040098
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x06000DF7 RID: 3575 RVA: 0x00041CE0 File Offset: 0x000400E0
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.leftCompleted = true;
						if (this.parent.rightCompleted)
						{
							this.parent.OnCompleted();
						}
					}
				}

				// Token: 0x040007E7 RID: 2023
				private readonly CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest parent;
			}

			// Token: 0x02000266 RID: 614
			private class RightObserver : IObserver<TRight>
			{
				// Token: 0x06000DF8 RID: 3576 RVA: 0x00041D44 File Offset: 0x00040144
				public RightObserver(CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest parent)
				{
					this.parent = parent;
				}

				// Token: 0x06000DF9 RID: 3577 RVA: 0x00041D54 File Offset: 0x00040154
				public void OnNext(TRight value)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.rightStarted = true;
						this.parent.rightValue = value;
						this.parent.Publish();
					}
				}

				// Token: 0x06000DFA RID: 3578 RVA: 0x00041DB4 File Offset: 0x000401B4
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.OnError(error);
					}
				}

				// Token: 0x06000DFB RID: 3579 RVA: 0x00041DFC File Offset: 0x000401FC
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						this.parent.rightCompleted = true;
						if (this.parent.leftCompleted)
						{
							this.parent.OnCompleted();
						}
					}
				}

				// Token: 0x040007E8 RID: 2024
				private readonly CombineLatestObservable<TLeft, TRight, TResult>.CombineLatest parent;
			}
		}
	}
}
